// ignore_for_file: library_private_types_in_public_api, must_be_immutable, use_build_context_synchronously

import 'package:flutter/material.dart';
import 'package:flutter_styled_toast/flutter_styled_toast.dart';
import 'package:intl/intl.dart';
import 'package:storage/database.dart';
import 'package:storage/model/user_name_entity.dart';
import 'package:storage/widget/double_input_formatter.dart';
import 'package:storage/widget/int_input_formatter.dart';

class AddAndEditGoodsDialog extends StatefulWidget {
  AddAndEditGoodsDialog({super.key, this.model, required this.refreshData});

  UserNameEntity? model;
  Future<void> Function() refreshData;

  @override
  _AddAndEditGoodsDialogState createState() => _AddAndEditGoodsDialogState();
}

class _AddAndEditGoodsDialogState extends State<AddAndEditGoodsDialog> {
  TextEditingController goodsNameController = TextEditingController();
  TextEditingController goodsSpecificationController = TextEditingController();
  TextEditingController goodsPriceController = TextEditingController();
  TextEditingController goodsCountController = TextEditingController();
  TextEditingController warehouseInTimeController = TextEditingController();

  @override
  void initState() {
    if (widget.model == null) {
      warehouseInTimeController.text =
          DateFormat('yyyy-MM-dd HH:mm:ss').format(DateTime.now());
    } else {
      goodsNameController.text = widget.model!.goodsName ?? '';
      goodsSpecificationController.text = widget.model!.goodsSpecification ?? '';
      goodsPriceController.text = (widget.model!.goodsPrice ?? 0).toString();
      goodsCountController.text =
          (widget.model!.inventoryQuantity ?? 0).toString();
      warehouseInTimeController.text = widget.model!.warehouseInTime ?? '';
    }

    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(20),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          SizedBox(
            width: 400,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                const Text('商品名称'),
                const SizedBox(width: 10),
                Expanded(
                    child: TextField(
                  controller: goodsNameController,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(
                        borderSide:
                            const BorderSide(width: 2.0, color: Colors.black),
                        // 边框样式
                        borderRadius: BorderRadius.circular(10.0), // 设置圆角半径
                      ),
                      hintText: '填写商品名称',
                      hintStyle: const TextStyle(color: Colors.grey)),
                )),
              ],
            ),
          ),
          const SizedBox(height: 10),SizedBox(
            width: 400,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                const Text('商品规格'),
                const SizedBox(width: 10),
                Expanded(
                    child: TextField(
                  controller: goodsSpecificationController,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(
                        borderSide:
                            const BorderSide(width: 2.0, color: Colors.black),
                        // 边框样式
                        borderRadius: BorderRadius.circular(10.0), // 设置圆角半径
                      ),
                      hintText: '填写商品规格',
                      hintStyle: const TextStyle(color: Colors.grey)),
                )),
              ],
            ),
          ),
          const SizedBox(height: 10),
          SizedBox(
            width: 400,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                const Text('商品价格'),
                const SizedBox(width: 10),
                Expanded(
                    child: TextField(
                  controller: goodsPriceController,
                  inputFormatters: [DoubleInputFormatter()],
                  decoration: InputDecoration(
                      border: OutlineInputBorder(
                        borderSide:
                            const BorderSide(width: 2.0, color: Colors.black),
                        // 边框样式
                        borderRadius: BorderRadius.circular(10.0), // 设置圆角半径
                      ),
                      hintText: '填写商品价格',
                      hintStyle: const TextStyle(color: Colors.grey)),
                )),
              ],
            ),
          ),
          const SizedBox(height: 10),
          SizedBox(
            width: 400,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                const Text('库存数量'),
                const SizedBox(width: 10),
                Expanded(
                    child: TextField(
                  readOnly: widget.model != null,
                  controller: goodsCountController,
                  inputFormatters: [IntegerInputFormatter()],
                  decoration: InputDecoration(
                      border: OutlineInputBorder(
                        borderSide:
                            const BorderSide(width: 2.0, color: Colors.black),
                        // 边框样式
                        borderRadius: BorderRadius.circular(10.0), // 设置圆角半径
                      ),
                      hintText: '填写库存数量',
                      hintStyle: const TextStyle(color: Colors.grey)),
                )),
              ],
            ),
          ),
          const SizedBox(height: 10),
          SizedBox(
            width: 400,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                const Text('入库时间'),
                const SizedBox(width: 10),
                Expanded(
                    child: TextField(
                  controller: warehouseInTimeController,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(
                        borderSide:
                            const BorderSide(width: 2.0, color: Colors.black),
                        // 边框样式
                        borderRadius: BorderRadius.circular(10.0), // 设置圆角半径
                      ),
                      hintText: '填写入库时间',
                      hintStyle: const TextStyle(color: Colors.grey)),
                )),
              ],
            ),
          ),
          const SizedBox(height: 10),
          const SizedBox(
            height: 20,
          ),
          SizedBox(
            width: 400,
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Expanded(
                    child: FilledButton(
                        onPressed: () {
                          Navigator.pop(context);
                        },
                        child: const Text('取消'))),
                const SizedBox(
                  width: 20,
                ),
                Expanded(
                    child: FilledButton(
                        onPressed: () async {
                          if (goodsNameController.text.isEmpty) {
                            showToast('请填写商品名称', context: context);
                            return;
                          } else if (goodsPriceController.text.isEmpty) {
                            showToast('请填写商品价格', context: context);
                            return;
                          } else if (goodsCountController.text.isEmpty) {
                            showToast('请填写商品数量', context: context);
                            return;
                          } else if (warehouseInTimeController.text.isEmpty) {
                            showToast('请填写入库时间', context: context);
                            return;
                          }
                          UserNameEntity? tempModel;
                          if (widget.model == null) {
                            tempModel = UserNameEntity(
                              goodsName: goodsNameController.text,
                              goodsSpecification: goodsSpecificationController.text,
                              goodsPrice:
                                  double.parse(goodsPriceController.text),
                              inventoryQuantity:
                                  int.parse(goodsCountController.text),
                              warehouseInTime: warehouseInTimeController.text,
                            );
                            await DatabaseHelper().addGoods(tempModel);
                          } else {
                            double? tempPrice = widget.model!.goodsPrice;
                            tempModel = widget.model;
                            tempModel!.goodsName = goodsNameController.text;
                            tempModel.goodsSpecification = goodsSpecificationController.text;
                            tempModel.goodsPrice =
                                double.parse(goodsPriceController.text);
                            tempModel.inventoryQuantity =
                                int.parse(goodsCountController.text);
                            tempModel.warehouseInTime ==
                                warehouseInTimeController.text;
                            tempModel.lastEditPrice = tempPrice;
                            await DatabaseHelper().editGood(tempModel);
                          }
                          widget.refreshData();
                          Navigator.pop(context);
                        },
                        child: const Text('确定'))),
              ],
            ),
          )
        ],
      ),
    );
  }
}
